//
//  ViewController.m
//  CoreDataAddressBook
//
//  Created by dllo on 15/11/20.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ViewController.h"
#import "FirstViewController.h"
#import "WXTableViewCell.h"
#import "WXCoreDataManager.h"
#import "Entity.h"
#import "SecondViewController.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSArray *array;
@property(nonatomic, strong)WXCoreDataManager *manager;
@end

@implementation ViewController
- (void)viewWillAppear:(BOOL)animated{
//    self.array = [NSMutableArray array];
//    [self.array addObjectsFromArray:[self select]];
    self.array = [self select];
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(addButtonAction:)] autorelease];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:(UITableViewStylePlain)];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[WXTableViewCell class] forCellReuseIdentifier:@"reuse"];
    self.tableView.rowHeight = 60;
    
    self.manager = [WXCoreDataManager defaults];
    
    
    
}
- (NSArray *)select{
    // 获取查询数据的请求, 相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"AddressBook"];
    
    // 添加约束 谓词
    NSPredicate *predicate = nil;
    request.predicate = predicate;
    
    //通过某个key给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    return array;
}

- (void)addButtonAction:(UIBarButtonItem *)button{
    FirstViewController *firstVC = [[FirstViewController alloc] init];
    [self.navigationController pushViewController:firstVC animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
//    cell.textLabel.text = @"22";
    cell.nameLabel.text = [self.array[indexPath.row] name];
    cell.sexLabel.text = [self.array[indexPath.row] sex];
    cell.telephotoLabel.text = [self.array[indexPath.row] telephone];
    
    return cell;
}



//重写系统的编辑按钮触发方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}
// 设置哪些行可以进行编辑操作
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //这个方法默认就是所有行都可以进行编辑
    return YES;
}
// 修改cell的编辑模式, 默认是删除,还可以进行添加等操作
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return  UITableViewCellEditingStyleDelete;
}
// 实现删除功能, 自带了一个左划显示删除功能的效果
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"删除");
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewRowAction *secondAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        [self.manager.managedObjectContext deleteObject:self.array[indexPath.row]];
        
        [self.manager saveContext];
     
        self.array = [self select];
        
        [self.tableView reloadData];

    }];
    return @[secondAction];
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SecondViewController *secVC = [[SecondViewController alloc] init];
    secVC.name = [self.array[indexPath.row] name];
    secVC.sex = [self.array[indexPath.row] sex];
    secVC.number = [self.array[indexPath.row] number];
    secVC.telephoto = [self.array[indexPath.row] telephone];
    secVC.qqNumber = [self.array[indexPath.row] qq];
    secVC.address = [self.array[indexPath.row] address];
    secVC.i = indexPath.row;
    [self.navigationController pushViewController:secVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
